package com.edushare.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;


import com.edushare.R;
import com.samsung.samm.common.SOptionPlay;
import com.samsung.samm.common.SOptionSCanvas;
import com.samsung.spensdk.SCanvasView;
import com.samsung.spensdk.applistener.AnimationProcessListener;
import com.samsung.spensdk.applistener.SCanvasInitializeListener;

public class AnimationActivity extends Activity {
	public static final String SAMM_DATA_KEY = "samm_data";
	private SCanvasView mCanvasView;
	private Button mBackButton;
	private Button mPlayButton;
	private Button mSaveButton;
	private ProgressBar mProgressBar;
	private boolean mIsAnimationFinished = false;

	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.animation);
		initCanvas();
		initViews();
	}

	@Override
	public void onDestroy() {
		mCanvasView.doAnimationClose();
		super.onDestroy();
	}

	@Override
	public void onBackPressed() {
		if (mIsAnimationFinished) {
			finish();
		}
	}

	/**
	 * Initializes the canvas object.
	 */
	private void initCanvas() {
		mCanvasView = (SCanvasView) findViewById(R.id.canvas_view);
		mCanvasView.setSCanvasInitializeListener(mSCanvasInitializeListener);
		mCanvasView.setAnimationProcessListener(mAnimationProcessListener);
		SOptionSCanvas option = new SOptionSCanvas();
		option.mPlayOption.setAnimationSpeed(SOptionPlay.ANIMATION_SPEED_AUTO);
		mCanvasView.setOption(option);
	}

	/**
	 * Listener for SCanvasView events.
	 */
	private final SCanvasInitializeListener mSCanvasInitializeListener = new SCanvasInitializeListener() {
		@Override
		public void onInitialized() {
			mCanvasView.setAnimationMode(true);
			Bundle extras = getIntent().getExtras();
			if (extras != null) {
				mCanvasView.loadSAMMData(extras.getString(SAMM_DATA_KEY));
				mCanvasView.doAnimationStart();
			}
		}
	};

	/**
	 * Initializes all visible views.
	 */
	private void initViews() {
		TextView appNameText = (TextView) findViewById(R.id.app_name_text);
		appNameText.setTypeface(Typeface.createFromAsset(getAssets(),
				"fonts/Roboto-Regular.ttf"));
		mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
		mBackButton = (Button) findViewById(R.id.back_button);
		mBackButton.setOnClickListener(mBackButtonListener);
		mPlayButton = (Button) findViewById(R.id.play_button);
		mPlayButton.setOnClickListener(mPlayButtonListener);
		mSaveButton = (Button) findViewById(R.id.save_button);
		mSaveButton.setOnClickListener(mSaveButtonListener);
	}

	/**
	 * Listener for changing animation progress and finishing animation
	 * playback.
	 */
	private final AnimationProcessListener mAnimationProcessListener = new AnimationProcessListener() {

		@Override
		public void onChangeProgress(int nProgress) {
			if (nProgress != 100) {
				mIsAnimationFinished = false;
				mPlayButton.setBackgroundResource(R.drawable.play_btn_pressed);
			}
			mProgressBar.setProgress(nProgress);
		}

		@Override
		public void onPlayComplete() {
			mIsAnimationFinished = true;
			mPlayButton.setBackgroundResource(R.drawable.play_btn);
		}
	};

	/**
	 * Listener for clicking "back" button.
	 */
	private final OnClickListener mBackButtonListener = new OnClickListener() {
		@Override
		public void onClick(final View v) {
			AnimationActivity.this.finish();
		}
	};

	/**
	 * Listener for clicking "play" button.
	 */
	private final OnClickListener mPlayButtonListener = new OnClickListener() {
		@Override
		public void onClick(final View v) {
			if (mIsAnimationFinished) {
				mCanvasView.doAnimationStart();
			}
		}
	};

	/**
	 * Listener for clicking "save" button.
	 */
	private final OnClickListener mSaveButtonListener = new OnClickListener() {
		@Override
		public void onClick(final View v) {
			Intent intent = new Intent(AnimationActivity.this,
					SaveActivity.class);
			startActivity(intent);
		}
	};
}
